﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.SeekHelp
{
    public class PartySeekHelp : EntityBase
    {

        #region 属性

        /// <summary>
        /// 求助人
        /// </summary>
        [StringLength(128)]
        public string SPerson { get; set; }


        /// <summary>
        /// 联系方式
        /// </summary>
        [StringLength(128)]
        public string ContactInformation { get; set; }


        /// <summary>
        /// 联系地址
        /// </summary>
        [StringLength(128)]
        public string Address { get; set; }


        /// <summary>
        /// 地图坐标
        /// </summary>
        [StringLength(128)]
        public string MapCoordinates { get; set; }


        /// <summary>
        /// 类型
        /// </summary>
        [StringLength(128)]
        public string Type { get; set; }


        /// <summary>
        /// 名称
        /// </summary>
        [StringLength(128)]
        public string Name { get; set; }


        /// <summary>
        /// 数量
        /// </summary>
        public int Num { get; set; }


        /// <summary>
        /// 求助有效期
        /// </summary>
        public DateTime EffectiveDate { get; set; }


        /// <summary>
        /// 状态
        /// </summary>
        public int State { get; set; }


        /// <summary>
        /// 描述
        /// </summary>
        [StringLength(1024)]
        public string Content { get; set; }


        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }


        /// <summary>
        /// 创建人
        /// </summary>
        [StringLength(128)]
        public string CreateBy { get; set; }


        #endregion

        #region 查询管理
        /// <summary>
        /// 查询管理
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="CreateBy">创建人</param>
        /// <param name="searchKey">名称</param>
        /// <param name="type">类型</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        public static DataSet GetListData(int pageIndex, int pageSize, string CreateBy, string searchKey, string type, string state)
        {
            #region 参数化
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter() {
                    DbType=DbType.String,
                    ParameterName="CreateBy",
                    Value=CreateBy
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            }; 
            #endregion

            StringBuilder sb = new StringBuilder();

            sb.Append(@"select SQL_CALC_FOUND_ROWS 	CASE State WHEN 1 THEN '未操作' WHEN 2 THEN '已预约' WHEN 3 THEN '已捐款' END as ViewState, CASE Type WHEN 1 THEN '款项' WHEN 2 THEN '物品' WHEN 3 THEN '服务' WHEN 4 THEN '其他' END as ViewType, ID,	SPerson,	ContactInformation,	Address,	MapCoordinates,	Type,	(Name),	Num,	EffectiveDate,	State,	Content,	CreateTime,	CreateBy from tblpartyseekhelp where CreateBy=@CreateBy ");

            #region 查询条件
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and (Name) like ?sName ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "sName", Value = string.Format("%{0}%", searchKey) });
            }
            if (!string.IsNullOrWhiteSpace(type))
            {
                sb.Append(" and Type=@Type ");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "Type", Value = type });
            }
            if (!string.IsNullOrWhiteSpace(state))
            {
                sb.Append(" and State=@State ");
                parameters.Add(new MySqlParameter() { DbType = DbType.Int32, ParameterName = "State", Value = Convert.ToInt32(state) });
            }
            #endregion

            sb.Append(" order by CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion

    }
}
